/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.pay.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.secure.BladeUser;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.api.ResultCode;
import com.ocm.core.tool.utils.BeanUtil;
import com.ocm.core.tool.utils.Func;
import com.ocm.pay.dto.CertificationDTO;
import com.ocm.pay.dto.RechargeDTO;
import com.ocm.pay.dto.WithdrawDTO;
import com.ocm.pay.service.IPayService;
import com.ocm.pay.vo.CertificationVO;
import com.ocm.pay.vo.SWalletVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.pay.entity.ShopPayinfo;
import com.ocm.pay.vo.ShopPayinfoVO;
import com.ocm.pay.wrapper.ShopPayinfoWrapper;
import com.ocm.pay.service.IShopPayinfoService;
import com.ocm.core.boot.ctrl.BladeController;

import java.math.BigDecimal;

import static com.ocm.pay.constant.PayTypeConstant.PAY_TYPE_SHOP;

/**
 *  控制器
 *
 * @author HQY
 * @since 2021-12-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shoppayinfo")
@Api(value = "", tags = "接口")
public class ShopPayinfoController extends BladeController {

	private final IShopPayinfoService shopPayinfoService;

	private final IPayService payService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入shopPayinfo")
	public R<ShopPayinfoVO> detail(ShopPayinfo shopPayinfo) {
		ShopPayinfo detail = shopPayinfoService.getOne(Condition.getQueryWrapper(shopPayinfo));
		return R.data(ShopPayinfoWrapper.build().entityVO(detail));
	}

	/**
	 * 充值
	 * @param rechargeDTO
	 * @param bladeUser
	 * @return
	 */
	@PostMapping("/s/recharge")
	@ApiOperationSupport(order = 1)
	@Transactional
	@ApiOperation(value = "充值", notes = "传入充值金额")
	public R<Boolean> uRecharge(@Valid @RequestBody RechargeDTO rechargeDTO, BladeUser bladeUser) {
		return R.status(payService.recharge(bladeUser.getShopId(),bladeUser.getUserName(),PAY_TYPE_SHOP,rechargeDTO.getRechargeAmount(),
			null,null,rechargeDTO.getPayPassword(),rechargeDTO.getCardId(),rechargeDTO.getBankType(),rechargeDTO.getBankCard(),rechargeDTO.getCardType(),null));
	}

	/**
	 * 提现
	 * @param withdrawDTO
	 * @param bladeUser
	 * @return
	 */
	@PostMapping("/s/withdraw")
	@ApiOperationSupport(order = 1)
	@Transactional
	@ApiOperation(value = "提现", notes = "传入提现金额")
	public R<Boolean> uWithdraw(@Valid @RequestBody WithdrawDTO withdrawDTO, BladeUser bladeUser) {
		return R.status(payService.withdraw(bladeUser.getShopId(),bladeUser.getUserName(),PAY_TYPE_SHOP,withdrawDTO.getWithdrawAmount(),
			withdrawDTO.getCardId(),withdrawDTO.getBankType(),withdrawDTO.getBankCard(),withdrawDTO.getCardType(),withdrawDTO.getPayPassword(),null));
	}


	@GetMapping("/s/certification")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取实名信息", notes = "")
	public R<CertificationVO> uDetail(BladeUser bladeuser) {
		ShopPayinfo detail = shopPayinfoService.getOne(new QueryWrapper<ShopPayinfo>().eq("shop_id",bladeuser.getShopId()).eq("is_deleted",0));
		CertificationVO certificationVO = new CertificationVO();

		//脱敏处理
		certificationVO.setCodeId(DesensitizedUtil.idCardNum(detail.getCodeId(),4,2));
		certificationVO.setCodeName(DesensitizedUtil.chineseName(detail.getCodeName()));
		return R.data(certificationVO);
	}

	/**
	 * 获取钱包信息
	 */
	@GetMapping("/s/wallet")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取钱包信息")
	public R<SWalletVO> uWallet(BladeUser bladeUser) {
		ShopPayinfo one = shopPayinfoService.getOne(new QueryWrapper<ShopPayinfo>().eq("shop_id", bladeUser.getShopId()).eq("is_deleted", 0));
		if(one==null){
			return R.fail(ResultCode.PAY_UN_CERTIFICATION);
		}
		SWalletVO w = BeanUtil.copy(one, SWalletVO.class);
		return R.data(w);
	}
	/**
	 * 实名认证
	 */
	@PostMapping("/s/certification")
	@ApiOperationSupport(order = 1)
	@Transactional
	@ApiOperation(value = "实名认证", notes = "传入userPayinfo")
	public R<Boolean> uCertification(@Valid @RequestBody CertificationDTO certificationDTO, BladeUser bladeUser) {
		if(shopPayinfoService.count(new QueryWrapper<ShopPayinfo>().eq("shop_id",bladeUser.getShopId()).eq("is_deleted",0))>0){
			return R.fail("该账号已实名");
		}
		ShopPayinfo u = BeanUtil.copy(certificationDTO, ShopPayinfo.class);
		u.setShopId(bladeUser.getShopId());
		u.setBalance(new BigDecimal(0));
		u.setFreeze(new BigDecimal(0));
		u.setVersion(0L);
		u.setIsDeleted(0);
		u.setCredit(0L);
		return R.status(shopPayinfoService.save(u));
	}




	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入shopPayinfo")
	public R<IPage<ShopPayinfoVO>> list(ShopPayinfo shopPayinfo, Query query) {
		IPage<ShopPayinfo> pages = shopPayinfoService.page(Condition.getPage(query), Condition.getQueryWrapper(shopPayinfo));
		return R.data(ShopPayinfoWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入shopPayinfo")
	public R<IPage<ShopPayinfoVO>> page(ShopPayinfoVO shopPayinfo, Query query) {
		IPage<ShopPayinfoVO> pages = shopPayinfoService.selectShopPayinfoPage(Condition.getPage(query), shopPayinfo);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入shopPayinfo")
	public R save(@Valid @RequestBody ShopPayinfo shopPayinfo) {
		return R.status(shopPayinfoService.save(shopPayinfo));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入shopPayinfo")
	public R update(@Valid @RequestBody ShopPayinfo shopPayinfo) {
		return R.status(shopPayinfoService.updateById(shopPayinfo));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入shopPayinfo")
	public R submit(@Valid @RequestBody ShopPayinfo shopPayinfo) {
		return R.status(shopPayinfoService.saveOrUpdate(shopPayinfo));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(shopPayinfoService.deleteLogic(Func.toLongList(ids)));
	}


}
